package game.editer.data;

import game.data.ProjectRoleData;
import haxe.io.Bytes;
import game.assets.ABNDataFuture;
import game.data.FileData;
#if electron
import sys.io.File;
#end
import haxe.io.BytesOutput;
import haxe.io.Path;
import lime._internal.format.Deflate;

using StringTools;
using haxe.io.Path;

/**
 * 人物包数据打包支持
 */
class PackageBuildData {
	public var projectPath:String;

	public var outBytes:BytesOutput = new BytesOutput();

	public function new(path:String) {
		projectPath = path;
	}

	/**
	 * 获得输出路径
	 * @return String
	 */
	public function getOutputPath():String {
		var dir = Path.directory(projectPath);
		var name = Path.withoutExtension(Path.withoutDirectory(projectPath));
		var savePath = Path.join([dir, "dist", name + ".abn"]);
		return savePath;
	}

	public function build(f:Float->Void, abproj:String) {
		var name = Path.withoutExtension(Path.withoutDirectory(projectPath));
		// 读取资源列表
		// images sound *.abproj
		var dir = Path.directory(projectPath);
		var savePath = getOutputPath();
		var savePathDir = Path.directory(savePath);
		#if electron
		if (sys.FileSystem.exists(savePathDir) == false)
			sys.FileSystem.createDirectory(savePathDir);
		#end
		var list:List<FileData> = new List();
		getEntries(dir, list);

		var files = cast(EditerStage.world.role.assets.data, ProjectRoleData).getAssetsFiles();

		// 先筛选abproj列表
		var abprojetData:Array<ProjectRoleData> = [cast EditerStage.world.role.assets.data];
		for (entry in list) {
			if (entry.name.endsWith(".abproj")) {
				if (entry.name.withoutDirectory().withoutExtension() != EditerStage.projectPath.withoutDirectory().withoutExtension()) {
					var project = new ProjectRoleData(entry.data.toString());
					abprojetData.push(project);
					project.getAssetsFiles(files);
				}
			}
		}

		for (entry in list) {
			var isABProj = entry.name.endsWith(".abproj");
			var isHaxe = entry.name.endsWith(".hx");
			var entryName = entry.name.withoutDirectory().withoutExtension();
			if (isABProj || isHaxe || files.filter((f) -> Path.withoutExtension(f.path) == entryName).length > 0) {
				// entry.data = isABProj ? Deflate.compress(Bytes.ofString(abproj)) : Deflate.compress(entry.data);
				if (isABProj) {
					// 需要判断一下是否当前项目
					if (EditerStage.projectPath.withoutDirectory().withoutExtension() == entryName.withoutExtension().withoutDirectory()) {
						entry.data = Deflate.compress(Bytes.ofString(abproj));
					} else {
						entry.data = Deflate.compress(entry.data);
					}
				} else {
					entry.data = Deflate.compress(entry.data);
				}
				outBytes.writeInt32(Bytes.ofString(entry.name).length);
				outBytes.writeString(entry.name);
				outBytes.writeInt32(entry.data.length);
				outBytes.writeBytes(entry.data, 0, entry.data.length);
			}
		}
		var bytes = outBytes.getBytes();
		#if electron
		File.saveBytes(savePath, bytes);
		#end
	}

	public function getEntries(dir:String, list:haxe.ds.List<FileData>, inDir:String = null):Void {
		#if electron
		var files:Array<String> = sys.FileSystem.readDirectory(dir);
		if (inDir == null) {
			inDir = Path.withoutDirectory(dir);
		}
		for (file in files) {
			if (file == "dist" || file.indexOf(".") == 0)
				continue;
			var path = Path.join([dir, file]);
			if (sys.FileSystem.isDirectory(path)) {
				getEntries(path, list, Path.join([inDir, file]));
			} else {
				var bytes = File.getBytes(path);
				var entry:FileData = {
					name: Path.join([inDir, file]),
					data: bytes,
				};
				list.push(entry);
			}
		}
		#end
	}
}